<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=3.0&ak=lV71VE5gbgDbikWpzHPtu0NoiXSeNRLV"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理
            <small>体检机构地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名/详细地址" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                <el-table-column prop="phone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="coordinates" label="经纬度" align="center"></el-table-column>
                <el-table-column prop="img" label="机构图片" align="center"></el-table-column>
                <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构地址" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话">
                                    <el-input v-model="formData.phone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/institution/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input id="coordinatePoint1" v-model="formData.coordinates"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="详细地址">
                                    <el-input id="site1" v-model="formData.address"/>
                                </el-form-item>
                            </el-col>
                            <el-button type="primary" icon="el-icon-location-outline" @click="loo1()">设置定位</el-button>
                        </el-row>
                        <!--这是地图的div-->
                        <div id="allmap1" style="width: 70%;height: 200px;"></div>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑体检机构地址" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话">
                                    <el-input v-model="formData.phone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/institution/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="经纬度">
                                    <el-input id="coordinatePoint2" v-model="formData.coordinates"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="15">
                                <el-form-item label="详细地址">
                                    <el-input id="site2" v-model="formData.address"/>
                                </el-form-item>
                            </el-col>
                            <el-button type="primary" icon="el-icon-location-outline" @click="loo2()">设置定位</el-button>
                        </el-row>
                        <!--这是地图的div-->
                        <div id="allmap2" style="width: 70%;height: 200px;"></div>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            rules: {//校验规则
                name: [{required: true, message: '体检机构名为必填项', trigger: 'blur'}]
            },
            dataList: [],//列表数据
            formData: {//表单数据
                name: '',
                phone: '',
                coordinates: '',
                address: ''
            },
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false//控制编辑窗口显示/隐藏
        },
        created() {
            this.findPage();
        },
        methods: {
            loo1() {
                let _this =this;
                // 百度地图API功能
                let map = new BMap.Map("allmap1");
                let ll = this.formData.coordinates;
                let point =null;
                if (ll == null) {
                    point = new BMap.Point(117.3352, 31.8664);
                    map.centerAndZoom(point, 17);
                } else {
                    let longitude = ll.split(",")[0];
                    let latitude = ll.split(",")[1];
                    point = new BMap.Point(longitude, latitude);
                    map.centerAndZoom(point, 17);
                }
                map.addOverlay(new BMap.Marker(point));//定点坐标红点覆盖
                map.enableScrollWheelZoom(true);
                let geoc = new BMap.Geocoder();

                map.addEventListener("click", function (e) {
                    //通过点击百度地图，可以获取到对应的point, 由point的lng、lat属性就可以获取对应的经度纬度
                    let pt = e.point;
                    geoc.getLocation(pt, function (rs) {
                        //addressComponents对象可以获取到详细的地址信息
                        let addComp = rs.addressComponents;
                        let site = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                        //将对应的HTML元素设置值
                        _this.formData.address = site;
                        _this.formData.coordinates = (pt.lng).toFixed(4) + ',' + (pt.lat).toFixed(4);
                        $("#site1").val(site);
                        $("#coordinatePoint1").val((pt.lng).toFixed(4) + ',' + (pt.lat).toFixed(4));
                    });
                });
            },
            loo2() {
                let _this =this;
                // 百度地图API功能
                let map = new BMap.Map("allmap2");
                let ll = this.formData.coordinates;
                let point =null;
                if (ll == null) {
                    point = new BMap.Point(117.3352, 31.8664);
                    map.centerAndZoom(point, 14);
                } else {
                    let longitude = ll.split(",")[0];
                    let latitude = ll.split(",")[1];
                    point = new BMap.Point(longitude, latitude);
                    map.centerAndZoom(point, 14);
                }

                map.addOverlay(new BMap.Marker(point));//定点坐标红点覆盖
                map.enableScrollWheelZoom(true);
                let geoc = new BMap.Geocoder();

                map.addEventListener("click", function (e) {
                    //通过点击百度地图，可以获取到对应的point, 由point的lng、lat属性就可以获取对应的经度纬度
                    let pt = e.point;
                    geoc.getLocation(pt, function (rs) {
                        //addressComponents对象可以获取到详细的地址信息
                        let addComp = rs.addressComponents;
                        let site = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                        //将对应的HTML元素设置值
                        _this.formData.address = site;
                        _this.formData.coordinates = (pt.lng).toFixed(4) + ',' + (pt.lat).toFixed(4);
                        $("#site2").val(site);
                        $("#coordinatePoint2").val((pt.lng).toFixed(4) + ',' + (pt.lat).toFixed(4));
                    });
                });
            },
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) { //http 200
                this.imageUrl = "http://qgc6wtosq.hd-bkt.clouddn.com/" + response.data;
                this.$message({
                    type: response.flag ? 'success' : 'error',
                    message: response.message
                });
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //添加
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        axios.post("/institution/add.do", this.formData).then((res) => {
                            this.dialogFormVisible = false;
                            if (res.data.flag) {
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                })
                            } else {
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            this.findPage();
                        })
                    } else {
                        //校验不通过
                        this.$message.error("数据校验不通过，请重新输入");
                        this.$refs['dataAddForm'].clearValidate();//清楚校验结果
                        return false;
                    }
                });
            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/institution/findPage.do", param).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.imageUrl = null;
                $("#allmap1").hide();
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },

            // 弹出编辑窗口
            handleUpdate(row) {
                //弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                $("#allmap2").show();
                //发送ajax请求，根据检查组ID查询当前检查组数据，用于基本信息的回显
                axios.get("/institution/findById.do?institutionId=" + row.id).then((res) => {
                    if (res.data.flag) {
                        //查询到数据
                        this.formData = res.data.data;
                        this.imageUrl = "http://qgc6wtosq.hd-bkt.clouddn.com/" + res.data.data.img;
                    } else {
                        //查询数据失败
                        this.$message.error(res.data.message);
                    }
                });
            },

            //编辑
            handleEdit() {
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //发送ajax请求，将修改后的表单数据（检查组基本信息、勾选的检查项）提交到后台进行处理
                        axios.post("/institution/edit.do",this.formData).then((res) => {
                            //关闭编辑窗口
                            this.dialogFormVisible4Edit = false;
                            if (res.data.flag) {
                                //操作成功
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            } else {
                                //操作失败
                                this.$message.error(res.data.message);
                            }
                        }).finally(() => {
                            this.findPage();
                        });
                    } else {
                        this.$message.error("表单数据校验失败");
                    }
                });
            },

            // 删除
            handleDelete(row) {
                axios.get("/institution/deleteById.do?institutionId=" + row.id).then((res => {
                    if (res.data.flag) {
                        //删除成功
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    } else {
                        //删除失败
                        this.$message.error(res.data.message);
                    }
                })).finally(() => {
                    //重新进行分页查询
                    this.findPage();
                });
            }
        }
    })
</script>
</html>
